mui.init({
})
//修改头像
mui.previewImage();
var save = document.getElementById('save');
var file = document.getElementById('file');
var back = document.getElementById('back');
var body = document.getElementById("body")
file.onchange = function() {
	mui.back()
	body.style.display = "none"
	var imgBox = document.getElementById('imgBox');
	document.getElementsByTagName('body')[0].style.backgroundColor = '#000';
	imgBox.style.display = 'block';
	file.style.display = "none"
};
save.onclick = function() {
	//	imgBox.style.display = 'none';
	//	body.style.display = "block"
	//	document.getElementsByTagName('body')[0].style.backgroundColor = '#f6f6f6';
	//	file.style.display = "none"
	//	请求后台修改
	imgBox.style.display = 'none';
	body.style.display = "block"
	document.getElementsByTagName('body')[0].style.backgroundColor = '#f6f6f6';
	file.style.display = "none"
	setTimeout(function() {
		common.jsonAjax('/userCenter/savePhoto', {
			imgData: $('#img').attr('src')
		}, function(data) {
			if(data.code == 1) {
				plus.webview.getWebviewById('my.html').reload()
			} else {
				if(data.resultMsg) {
					common.toast(data.resultMsg)
				} else {
					console.log(data)
				}
			}
		})
	}, 100)

}
back.onclick = function() {
	plus.webview.currentWebview().reload();
}

var back = mui.back
mui.back = function() {
	if($("#imgBox").css("display") == "block") {
		plus.webview.currentWebview().reload();
	} else {
		back()
	}
}
//$(".append").on("tap", function() {
//	plus.gallery.pick(function(path) {
//		$(".append").html("<div><img src='" + path + "'/></div>")
//	}, function(e) {
//		mui.toast("你取消了选择图片!");
//	}, {
//		filter: "image"
//	});
//})
var base64;
var cut = new ImgClip({
	canvas: 'canvas01', // canvas id
	fileObj: 'file', // file id
	cutBtn: 'save', // cut btn id
	resultObj: 'img', // result img i
	rotateR: 'rotateR',
	cutScale: 1, // 1:1、3:4
	cutW: '300' // '数字'或者'winW'关键字，裁切宽度，随屏幕宽或自己设置
});

//请求信息
var name;
userData()

function userData() {
	common.ajax('/userCenter/getUserDetail', {}, function(data) {
		if(data.code == 1) {
			name = data.data.nickName
			data.data.nickName != '' ? $(".name").val(data.data.nickName) : $(".name")[0].placeholder = '请输入姓名'
			data.data.birth != '' ? $("#date").text(data.data.birth) : $("#date").text('选择日期')
			data.data.defaultAddress != '' ? $(".checkres").html(data.data.defaultAddress) : $(".checkres").html('<span>请选择默认地址</span>')
			if(data.data.sex == 'M') {
				$("#sex_tap").text('男')
			} else if(data.data.sex == 'F') {
				$("#sex_tap").text('女')
			} else {
				$("#sex_tap").text('保密')
			}

			console.log(common.imageUrl + data.data.headImg)
			data.data.phone != '' ? $(".phone").text(data.data.phone) : ''
			data.data.headImg || data.data.headImg != '' ? $(".user_head img").attr('src', common.imageUrl + data.data.headImg) : ''

			data.data.vip ? $(".vip_num").text('V' + data.data.vip) : $("#vip").hide()
		} else {
			if(data.resultMsg) {
				common.toast(data.resultMsg)
			} else {
				console.log(data)
			}
		}
	}, false, 'get')
}
//修改姓名
$(".name").on('blur', function() {
	if(common.replace($(this).val()) == '') {
		$(this).val(name)
		return
	}
	var self = $(this)
	common.ajax('/userCenter/updateNickName', {
		nickName: $(this).val()
	}, function(data) {
		if(data.code == 1) {
			name = self.val()
			console.log(data)
			plus.webview.getWebviewById('my.html').reload()
		} else {
			if(data.resultMsg) {
				common.toast(data.resultMsg)
			} else {
				console.log(data)
			}
		}
	})
})
//选择性别
var mask = mui.createMask(hide); //callback为用户点击蒙版时自动执行的回调；
$("#sex_tap").on("tap", function() {
	if($('.sex').hasClass('alert')) {
		for(var i = 0; i < $(".sex div").length; i++) {
			if($(this).text() == $(".sex div").eq(i).text()) {
				$(".sex div").eq(i).addClass('sex_check').siblings().removeClass('sex_check')
			}
		}
		mask.show(); //显示遮罩
		$('.sex').removeClass('alert')
	} else {
		$('.sex').addClass('alert')
	}
})

$(".sex div").on('tap', function() {
	var self = $(this)
	if($(this).text() == $("#sex_tap").text()) {
		return
	}
	common.ajax('/userCenter/updateSex', {
		gender: self.text()
	}, function(data) {
		if(data.code == 1) {
			$("#sex_tap").text(self.text())
			self.addClass('sex_check').siblings().removeClass('sex_check')
			mask.close()
		} else {
			if(data.resultMsg) {
				common.toast(data.resultMsg)
			} else {
				console.log(data)
			}
		}
	})
})

function hide() {
	$('.sex').addClass('alert')
}

//选择日期
mui('#date')[0].addEventListener('tap', function() {
	var _self = this;
	var myDate = new Date();
	var options = {
		"type": "date",
		beginDate: new Date(1897, 00, 01), //设置开始日期 
		endDate: new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate()), //设置结束日期 
	}

	if($('#date').text() != '选择日期') {
		options.value = $('#date').text()
	}

	_self.picker = new mui.DtPicker(options);
	_self.picker.show(function(rs) {
		_self.picker.dispose();
		_self.picker = null;
		common.ajax('/userCenter/updateBirthday', {
			year: rs.y.text,
			month: rs.m.text,
			day: rs.d.text
		}, function(data) {
			if(data.code == 1) {
				$("#date").text(rs.text)
			} else {
				if(data.resultMsg) {
					common.toast(data.resultMsg)
				} else {
					console.log(data)
				}
			}
		})
	});
}, false);

$("#payPassword").on("tap", function() {
	common.open('../order/setpassword.html', 'setpassword.html')
})

window.addEventListener('refurbish', function() {
	userData()
});